/*
 * TwittersnepsView.java
 */

package cl.dfg.twittersneps;

import cl.dfg.twittersneps.modelo.Diccionario;
import java.awt.Desktop.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import edu.buffalo.sneps.JavaSnepsAPI;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.AWTException;
import java.util.List;


/**
 * The application's main frame.
 */
public class TwittersnepsView extends FrameView {
    public static JavaSnepsAPI api;
    private SystemTray tray;
    boolean tweet;
    private final TrayIcon trayIcon;
    static TwittersnepsApp sneps = new TwittersnepsApp();
    String rutaSneps;
    TwitterStream ts;
    Desktop desktop;
    File file;
    public static FileRW fileRW = new FileRW();
    Path path;
    public static String pathGATN;
    public static String pathLexicon;
    public static List <Diccionario> lexicon = new ArrayList<Diccionario>();

    public TwittersnepsView(SingleFrameApplication app) {
        super(app);        
        initComponents();
        Image image = Toolkit.getDefaultToolkit().getImage("twitter-logo.gif");
        trayIcon = new TrayIcon(image,"TwitterSNePS", null);
        desktop = Desktop.getDesktop();       
        path = new Path();
        rutaSneps = null;
        tweet = false;
        minimizeApp();
    }

    @Action
    public void showAboutBox() {
        TwittersnepsAboutBox aboutBox = null;
        if (aboutBox == null) {
           JFrame frame = TwittersnepsApp.getApplication().getMainFrame();
           aboutBox = new TwittersnepsAboutBox(frame);
           aboutBox.setLocationRelativeTo(frame);
        }
        TwittersnepsApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        statusPanel = new javax.swing.JPanel();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextPane3 = new javax.swing.JTextPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTextPane4 = new javax.swing.JTextPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTextPane5 = new javax.swing.JTextPane();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTextPane6 = new javax.swing.JTextPane();
        jScrollPane8 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane9 = new javax.swing.JScrollPane();
        jTextPane8 = new javax.swing.JTextPane();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane10 = new javax.swing.JScrollPane();
        jTextPane9 = new javax.swing.JTextPane();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane11 = new javax.swing.JScrollPane();
        jTextPane10 = new javax.swing.JTextPane();
        jScrollPane12 = new javax.swing.JScrollPane();
        jTextPane11 = new javax.swing.JTextPane();
        jScrollPane13 = new javax.swing.JScrollPane();
        jTextPane12 = new javax.swing.JTextPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        openGATNItem = new javax.swing.JMenuItem();
        openLexiconItem = new javax.swing.JMenuItem();
        cfgSNePS = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();

        mainPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(852, 30));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(TwittersnepsView.class);
        statusPanel.setBackground(resourceMap.getColor("statusPanel.background")); // NOI18N
        statusPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("statusPanel.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP)); // NOI18N
        statusPanel.setName("statusPanel"); // NOI18N
        statusPanel.setPreferredSize(new java.awt.Dimension(852, 30));

        statusMessageLabel.setText(resourceMap.getString("statusMessageLabel.text")); // NOI18N
        statusMessageLabel.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        statusMessageLabel.setName("statusMessageLabel"); // NOI18N
        statusMessageLabel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                statusMessageLabelComponentShown(evt);
            }
        });

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGap(866, 866, 866)
                .addComponent(statusAnimationLabel)
                .addContainerGap(91, Short.MAX_VALUE))
            .addComponent(statusMessageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 957, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(608, 608, 608)
                .addComponent(statusAnimationLabel))
        );

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTabbedPane1.setAutoscrolls(true);
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.setOpaque(true);
        jTabbedPane1.setPreferredSize(new java.awt.Dimension(852, 200));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextPane1.setName("jTextPane1"); // NOI18N
        jScrollPane1.setViewportView(jTextPane1);

        jTabbedPane1.addTab(resourceMap.getString("jScrollPane1.TabConstraints.tabTitle"), jScrollPane1); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextPane2.setName("jTextPane2"); // NOI18N
        jScrollPane3.setViewportView(jTextPane2);

        jTabbedPane1.addTab("Lexicon", jScrollPane3);

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTextPane3.setName("jTextPane3"); // NOI18N
        jScrollPane4.setViewportView(jTextPane3);

        jTabbedPane1.addTab("GATN", jScrollPane4);

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        jTextPane4.setName("jTextPane4"); // NOI18N
        jScrollPane5.setViewportView(jTextPane4);

        jTabbedPane1.addTab("WWFs", jScrollPane5);

        jScrollPane6.setName("jScrollPane6"); // NOI18N

        jTextPane5.setName("jTextPane5"); // NOI18N
        jScrollPane6.setViewportView(jTextPane5);

        jTabbedPane1.addTab("Clusters", jScrollPane6);

        jScrollPane7.setName("jScrollPane7"); // NOI18N

        jTextPane6.setName("jTextPane6"); // NOI18N
        jScrollPane7.setViewportView(jTextPane6);

        jTabbedPane1.addTab("Oportunidades/Amenazas", jScrollPane7);

        jScrollPane8.setName("jScrollPane8"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane9.setName("jScrollPane9"); // NOI18N

        jTextPane8.setName("jTextPane8"); // NOI18N
        jScrollPane9.setViewportView(jTextPane8);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane10.setName("jScrollPane10"); // NOI18N

        jTextPane9.setName("jTextPane9"); // NOI18N
        jScrollPane10.setViewportView(jTextPane9);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton2MouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(288, 288, 288))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)))
                        .addGap(42, 42, 42)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(189, 189, 189))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jButton2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane10)
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                .addContainerGap(138, Short.MAX_VALUE))
        );

        jScrollPane8.setViewportView(jPanel1);

        jTabbedPane1.addTab("Búsqueda reglas GATN", jScrollPane8);

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane11.setName("jScrollPane11"); // NOI18N

        jTextPane10.setName("jTextPane10"); // NOI18N
        jScrollPane11.setViewportView(jTextPane10);

        jScrollPane12.setName("jScrollPane12"); // NOI18N

        jTextPane11.setName("jTextPane11"); // NOI18N
        jScrollPane12.setViewportView(jTextPane11);

        jScrollPane13.setName("jScrollPane13"); // NOI18N

        jTextPane12.setName("jTextPane12"); // NOI18N
        jScrollPane13.setViewportView(jTextPane12);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane11, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane12, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane13, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(211, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addComponent(jScrollPane12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addComponent(jScrollPane11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE))
                .addGap(87, 87, 87))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jPasswordField1.setText(resourceMap.getString("jPasswordField1.text")); // NOI18N
        jPasswordField1.setName("jPasswordField1"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(statusPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 955, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(113, 113, 113)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName(resourceMap.getString("jTabbedPane1.AccessibleContext.accessibleName")); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        openGATNItem.setText(resourceMap.getString("openGATNItem.text")); // NOI18N
        openGATNItem.setName("openGATNItem"); // NOI18N
        openGATNItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openGATNItemActionPerformed(evt);
            }
        });
        fileMenu.add(openGATNItem);

        openLexiconItem.setText(resourceMap.getString("openLexiconItem.text")); // NOI18N
        openLexiconItem.setName("openLexiconItem"); // NOI18N
        openLexiconItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openLexiconItemActionPerformed(evt);
            }
        });
        fileMenu.add(openLexiconItem);

        cfgSNePS.setText(resourceMap.getString("cfgSNePS.text")); // NOI18N
        cfgSNePS.setName("cfgSNePS"); // NOI18N
        cfgSNePS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cfgSNePSActionPerformed(evt);
            }
        });
        fileMenu.add(cfgSNePS);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(TwittersnepsView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setDisabledIcon(resourceMap.getIcon("jMenuItem1.disabledIcon")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.setPreferredSize(new java.awt.Dimension(95, 22));
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jPopupMenu1.add(jSeparator1);

        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem3);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jPopupMenu1.add(jSeparator2);

        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.setPreferredSize(new java.awt.Dimension(95, 22));
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem2);

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
    // TODO add your handling code here:
        String username = new String(jTextField1.getText());
        String password = new String(jPasswordField1.getText());
	String search = new String(jTextField2.getText());

	        if(username.equals("") || password.equals("")|| search.equals("") ) // If password and username is empty > Do this >>>
	        {
                jButton1.setEnabled(false);
                statusMessageLabel.setText("Debe ingresar su usuario y password de twitter y la palabra a buscar para continuar.");
                jTextField1.setText("");
                jPasswordField1.setText("");
	            jButton1.setEnabled(true);
	        }
	        else{
	            try {
	                statusMessageLabel.setText("");
	                TwittersnepsThreads(username,password,search);
	            }
	            catch(Exception e) {
	                statusMessageLabel.setText(e.getMessage());
	            }
	        }
	     };

   public void newMessage(String message){
	         tweet = true;
	         trayIcon.displayMessage("New tweet", "From: "+message,TrayIcon.MessageType.INFO);
	    }


   private void minimizeApp() {
        // TODO add your handling code here:              
        if (SystemTray.isSupported()) {
            tray = SystemTray.getSystemTray();
            MouseListener mouseListener = new MouseListener( ) {
                public void mouseClicked(MouseEvent e) {
                    
                }
                public void mouseEntered(MouseEvent e) {

                }
                public void mouseExited(MouseEvent e) {

                }
                public void mousePressed(MouseEvent e) {
                }
                public void mouseReleased(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        jPopupMenu1.setLocation(e.getX(), e.getY());
                        jPopupMenu1.setInvoker(jPopupMenu1);
                        jPopupMenu1.setVisible(true);
                    }
                }
            };
            trayIcon.setImageAutoSize(true);
            trayIcon.addMouseListener(mouseListener);
            trayIcon.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //Esta accion ocurre cuando se hace click en el mensaje del popup                        
                        jMenuItem1ActionPerformed(e);
                    }
                }
            );

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("No se pudo agregar el ícono a la barra tray");                
                sneps.view.getFrame().setVisible(true);
                sneps.view.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        } else {
            //  System Tray is not supported
        }
    }

   public void TwittersnepsThreads(String username, String password, String search) throws IOException {


        File file = new File("kb.sneps");

        if(rutaSneps != null){
            //Datos de conexión API SNePs
            api = new JavaSnepsAPI(rutaSneps,5668);

            if(!api.isConnected()){
                statusMessageLabel.setText("La conección ha fallado!!");
                        System.out.println("La conección ha fallado!!");
            }
            else{
                statusMessageLabel.setText("La conección ha sido Exitosa!!");
                statusMessageLabel.repaint();

                System.out.println("La conección ha sido Exitosa!!");
            }

            boolean exists = file.exists();
            if (!exists) {
          // It returns false if File or directory does not exist
               statusMessageLabel.setText("Comenzando con una KB vacía");
                System.out.println("Comenzando con una KB vacía");
                }
            else{
          // It returns true if File or directory exists
                api.tell("^(sneps:innet \"kb.sneps\")");
                statusMessageLabel.setText("Comenzando con una KB existente:");
                System.out.println("Comenzando con una KB existente:");
                api.tell( "define-frame Tweet(when who where what support)");
             //  api.tell("^(sneps:lexin \"lexicon.lisp\")");

            }


            //Datos de conexión.
           TwitterStreamConfiguration tws = new TwitterStreamConfiguration(username, password);
            MyTwitterHandler handler = new MyTwitterHandler();
            Collection<String> tracks = new ArrayList<String>();
            //Palabra clave a buscar.
            tracks.add(search);
            ts = TweetRiver.filter(tws, handler, null, tracks);
            (new Thread(ts)).start();
            statusMessageLabel.setText("Conectado a Twitter...");
            System.out.println("Conectado a Twitter...");
            sneps.view.getFrame().setVisible(false);
            trayIcon.displayMessage("TwitterSNePS", "Searching...",TrayIcon.MessageType.INFO);


            
        }
        else{
            statusMessageLabel.setText("No ha indicado la ruta del archivo de configuración de SNePS, vaya al menú File para buscarlo");

        }
        
    }//GEN-LAST:event_jButton1MouseReleased
     
     
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
    // TODO add your handling code here:
        if(ts != null){
            ts.stop();
            api.tell("^(sneps:outnet \"kb.sneps\")");
            System.exit(0);
        }
        else{
            System.exit(0);
        }
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void statusMessageLabelComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_statusMessageLabelComponentShown
    // TODO add your handling code here:
    }//GEN-LAST:event_statusMessageLabelComponentShown

    private void openLexiconItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openLexiconItemActionPerformed
    // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        int resp = fc.showOpenDialog(openLexiconItem);
        lexicon = fileRW.leerLexicon();

        if(resp == JFileChooser.APPROVE_OPTION){
            file = fc.getSelectedFile();
        }
        if(file!=null){
            pathLexicon = file.getAbsolutePath();
            path.setName("Archivo Lexicon");
            path.setDescription("Archivo que contiene las definiciones del diccionario");
            path.setUrl(pathLexicon);
            String text = fileRW.readFile(pathLexicon);
            jTextPane2.setText(text);
        }

    }//GEN-LAST:event_openLexiconItemActionPerformed

    private void openGATNItemActionPerformed(java.awt.event.ActionEvent evt){//GEN-FIRST:event_openGATNItemActionPerformed
    // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        int resp = fc.showOpenDialog(openGATNItem);

        

        if(resp == JFileChooser.APPROVE_OPTION){
            file = fc.getSelectedFile();
        }
        if(file!=null){
            pathGATN = file.getAbsolutePath();
            path.setName("Archivo Lexicon");
            path.setDescription("Archivo que contiene las definiciones del diccionario");
            path.setUrl(pathGATN);
            String text = fileRW.readFile(pathGATN);
            jTextPane3.setText(text);
        }
        try {
            fileRW.leerGatn();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TwittersnepsView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_openGATNItemActionPerformed
     

    private void cfgSNePSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cfgSNePSActionPerformed
         // TODO add your handling code here:
         JFileChooser fc = new JFileChooser();
         int resp = fc.showOpenDialog(cfgSNePS);

         if(resp == JFileChooser.APPROVE_OPTION){
             file = fc.getSelectedFile();
         }         
         rutaSneps = file.getAbsolutePath();
     }//GEN-LAST:event_cfgSNePSActionPerformed

     private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased
         // TODO add your handling code here:
         if(pathGATN != null){
             jTextPane8.setText(fileRW.searchRuleFile(jTextField3.getText(),pathGATN));
         } else{
             statusMessageLabel.setText("No ha indicado la ruta del archivo GATN, vaya al menú File para buscarlo");
         }
     }//GEN-LAST:event_jButton2MouseReleased

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
	         // TODO add your handling code here:
	         if(!sneps.view.getFrame().isVisible()){
	             sneps.view.getFrame().setVisible(true);
	             sneps.view.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
	         }
	         //When a new tweet comes
	         if(tweet == true){
	             sneps.view.getFrame().setVisible(true);
	             sneps.view.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
	             jTabbedPane1.setSelectedComponent(jScrollPane1);
	             tweet = false;
         }
	         //When lexicon file change
	         //When GATN file change
	         //When something happens in WWFs
	         //When something happens in Clusters
	         //When something happens in Oportunidades y amenazas
	         //When something happens in Stats
	     }//GEN-LAST:event_jMenuItem1ActionPerformed
	
	     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
	         // TODO add your handling code here:
	         if(ts != null){
	            ts.stop();
	            api.tell("^(sneps:outnet \"kb.sneps\")");
	            System.exit(0);
	         }
	         else{
	            System.exit(0);
	         }
	     }//GEN-LAST:event_jMenuItem2ActionPerformed
	
	     private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
	         // TODO add your handling code here:
	         sneps.view.getFrame().setVisible(false);
	}//GEN-LAST:event_jMenuItem3ActionPerformed
	 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem cfgSNePS;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane11;
    private javax.swing.JScrollPane jScrollPane12;
    private javax.swing.JScrollPane jScrollPane13;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    public static javax.swing.JTextPane jTextPane1;
    public static javax.swing.JTextPane jTextPane10;
    public static javax.swing.JTextPane jTextPane11;
    public static javax.swing.JTextPane jTextPane12;
    private javax.swing.JTextPane jTextPane2;
    public javax.swing.JTextPane jTextPane3;
    public static javax.swing.JTextPane jTextPane4;
    private javax.swing.JTextPane jTextPane5;
    private javax.swing.JTextPane jTextPane6;
    private javax.swing.JTextPane jTextPane8;
    private javax.swing.JTextPane jTextPane9;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openGATNItem;
    private javax.swing.JMenuItem openLexiconItem;
    private javax.swing.JLabel statusAnimationLabel;
    javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    
}
